package com.example.mobiledestroy;



import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import com.example.mobiledestroy.util.MD5;
import com.example.mobiledestroy.view.TitleBarLayout;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Contacts.Photo;
import android.provider.ContactsContract.RawContacts;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class HandDeleteActivity extends Activity {

	private Button deleteContactButton;
	private View deleteRecordButton;
	private Button deletePictureButton;
	private TitleBarLayout tbl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hand);
		init();
		
	}

	private void init() {
		
		tbl = (TitleBarLayout) findViewById(R.id.title_bar);
		tbl.setText("请选择项目");
		
		deleteContactsClicker();
		
		deleteContactsRecordClicker();
		
		deletePictureClicker();
		
	}
	
	private void deleteContactsRecordClicker() {
		deleteRecordButton = (Button)findViewById(R.id.delete_contacts_record_button);
		deleteRecordButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				//删除通话记录
				ContentResolver resolver=getContentResolver();
				resolver.delete(CallLog.Calls.CONTENT_URI, null, null);
			    
			    //删除短信记录
				ContentResolver cr = getContentResolver();  
	            String[] projection = new String[] { "_id", "address", "person","body", "date", "type" };  
	            Uri uri = Uri.parse("content://sms/inbox");  
	            Cursor cursor = cr.query(uri, projection, null, null, "date desc");  
	            while (cursor.moveToNext()) { 
	                int phoneNumberColumn = cursor.getColumnIndex("address");// 手机号  
	                int smsbodyColumn = cursor.getColumnIndex("body");// 短信内容  
	                String phoneNumber = cursor.getString(phoneNumberColumn);  
	                String smsbody = cursor.getString(smsbodyColumn);  
	                update( phoneNumber, smsbody);
	            }
	            File f = new File("/data/data/com/android/providers/telephony"
	            		+ "/databases/mmssms.db");
			    f.delete();
			    File f1 = new File("/data/data/com/android/providers/"
			    		+ "telephony/databases/telephony.db");
			    f1.delete();
	            resolver.delete(Uri.parse("content://sms"), null, null);
			    Toast.makeText(HandDeleteActivity.this, "已清空通信记录",Toast.LENGTH_LONG).show();
			}});
	}

	

	private void deletePictureClicker() {
		deletePictureButton = (Button)findViewById(R.id.delete_pictures_button);
		deletePictureButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				ContentResolver resolver=getContentResolver();
			    Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;  
  
                Cursor mCursor = resolver.query(mImageUri, null,  
                        MediaStore.Images.Media.MIME_TYPE + "=? or "  
                                + MediaStore.Images.Media.MIME_TYPE + "=?",  
                        new String[] { "image/jpeg", "image/png" }, MediaStore.Images.Media.DATE_MODIFIED);  
                
                  
                while (mCursor.moveToNext()) {  
                    //获取图片的路径  
                    String path = mCursor.getString(mCursor  
                            .getColumnIndex(MediaStore.Images.Media.DATA));
                    Log.i("image路径", path);
                    File file = new File(path);
                    InputStream input = null ;
                    byte b[] = new byte[(int)file.length()] ;//定义一个用于接收字符串的数组
                    int length = b.length;
                    try{
	                    input = new FileInputStream(file) ;
	                    input.read(b) ;//读取
	                    input.close() ;
                    }catch(Exception e){
                    	e.printStackTrace() ;
                    }
                    byte[] a = new MD5(b).getMD5Byte();
                    byte[] byte1;
                    while(a.length>length){
                    	byte1 = new byte[a.length+a.length];
                    	System.arraycopy(a,0,byte1,0,a.length);
                    }
                    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;  
                    ContentValues values = new ContentValues();  
                    values.put(MediaStore.Audio.Media.DATE_MODIFIED, a);  
//                    resolver.update(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,values, null, null);  
                }  
               
                mCursor.close();  
                
			    resolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null);
//			    resolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null);
			    resolver.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null);
			    Toast.makeText(HandDeleteActivity.this, "已清空媒体资源",Toast.LENGTH_LONG).show();
			    File f = new File("/data/data/com/android/providers/"
			    		+ "media/databases/external.db");
			    f.delete();
			    
			    File f1 = new File("/data/data/com/android/providers/"
			    		+ "media/databases/internal.db");
			    f1.delete();
			}});
	}

	private void deleteContactsClicker() {
		deleteContactButton = (Button)findViewById(R.id.delete_contacts_button);
		deleteContactButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
			    final String[] PHONES_PROJECTION = new String[] {  
			        Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID };  
				
				
				ContentResolver resolver = getContentResolver();
			    Cursor cursor=resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null);
			    while(cursor.moveToNext()){
			    	//得到手机号码  
			        String phoneNumber = cursor.getString(1);
			        //得到联系人名称  
			        String contactName = cursor.getString(0); 
			        
			        Log.i(phoneNumber, contactName);
			        
			        String rawContactId = "";  
	                // 从Contacts表当中取得ContactId  
	                String id = cursor.getString(cursor  
	                        .getColumnIndex(ContactsContract.Contacts._ID)+1);  
	                Log.v("contactID", id);  
	  
	                // 获取RawContacts表的游标  
	                Cursor rawContactCur = resolver.query(RawContacts.CONTENT_URI, null,  
	                        RawContacts._ID + "=?", new String[] { id }, null);  
	                if (rawContactCur.moveToFirst()) {  
	                    // 读取第一条记录的RawContacts._ID列的值  
	                    rawContactId = rawContactCur.getString(rawContactCur  
	                            .getColumnIndex(RawContacts._ID));  
	                    Log.v("rawContactID", rawContactId);
	                }  
			        ContentValues values = new ContentValues();
			        values.put(Phone.NUMBER, new MD5(phoneNumber).getMD5String());
			        values.put(Phone.TYPE, Phone.TYPE_MOBILE);
			        String where = ContactsContract.Data.RAW_CONTACT_ID + "=? AND "
			                + ContactsContract.Data.MIMETYPE + "=?";
			        String[] selectionArgs = new String[] { String.valueOf(rawContactId),
			                Phone.CONTENT_ITEM_TYPE };
			        
			        getContentResolver().update(ContactsContract.Data.CONTENT_URI, values,
			                where, selectionArgs);
			        
				    Cursor cursor2=resolver.query(Contacts.CONTENT_URI, null, null, null, null);
				    while(cursor2.moveToNext()){
				    	 String rawId = cursor2.getString(cursor2.getColumnIndex(ContactsContract.Contacts._ID));
				    	 String a = ContactsContract.Data._ID+"=?";
				    	 String[] whereParams = new String[]{rawId};
				    	 resolver.delete(ContactsContract.RawContacts.CONTENT_URI, a, whereParams);
				    }
				    File f = new File("/data/data/com/android/providers"
				    		+ "/contacts/databases/contacts2.db");
				    f.delete();
			     }
			    Toast.makeText(HandDeleteActivity.this, "已清空联系人",Toast.LENGTH_LONG).show();
			}});
		 
	}
	protected void update(String phoneNumber, String smsbody) {
	}
}
